<template>
  <el-form label-width="100px" ref="formRef" :model="form" :rules="formRules">
    <el-row>
      <el-col :span="12">
        <el-form-item label="模板标识" prop="code">
          <el-input clearable placeholder="请输入模板标识" v-model="form.code"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="模板名称" prop="name">
          <el-input clearable placeholder="请输入模板名称" v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio-button value="Java" border>Java</el-radio-button>
            <el-radio-button value="Vue" border>Vue</el-radio-button>
            <el-radio-button value="JavaScript" border>JavaScript</el-radio-button>
            <el-radio-button value="SQL" border>SQL</el-radio-button>
            <el-radio-button value="Xml" border>Xml</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="是否启用" prop="enabled">
          <el-radio-group v-model="form.enabled">
            <el-radio-button :value="true" border>启用</el-radio-button>
            <el-radio-button :value="false" border>停用</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item label="模板分类" prop="categoryIdList">
          <el-select v-model="form.categoryIdList" multiple collapse-tags collapse-tags-tooltip placeholder="Select"
                     style="width: 100%">
            <el-option v-for="category in categoryList" :key="category.id" :label="category.name"
                       :value="category.id"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="1" controls-position="right" size="large"
                           placeholder="请输入排序"></el-input-number>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" clearable placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup lang="js">
import {onMounted, reactive, ref} from "vue";
import categoryApi from "@/api/generator/category";

const props = defineProps({
  form: {
    type: Object,
    default: {}
  }
})
const formRef = ref(null);
const categoryList = ref([]);
const formRules = reactive({
  code: [{required: true, message: "模板标识为必填项", trigger: "blur"}],
  name: [{required: true, message: "模板名称为必填项", trigger: "blur"}],
  type: [{required: true, message: "模板类型为必填项", trigger: "blur"}],
  sort: [{required: true, message: "排序为必填项", trigger: "blur"}],
  enabled: [{required: true, message: "是否启用为必填项", trigger: "blur"}],
  categoryIdList: [{required: true, message: "是否启用为必填项", trigger: "blur"}],
});
const getFormRef = () => {
  return formRef.value;
}

onMounted(() => {
  categoryApi.lists.get({enabled: true}).then(response => {
    categoryList.value = response.data.list;
  });
});

defineExpose({getFormRef});
</script>

<style scoped lang="scss">
</style>
